package xdu.lz.stage7_jucCollection.blockingqueue;

import java.util.LinkedHashMap;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created at 2019/10/28 0028 下午 11:19
 * --
 */
public class PC_BlockingQueue {


    private static BlockingQueue<Integer> queue = new LinkedBlockingDeque<>(100);

    private static int counter = 0;

    public void produce(){
        try {

            queue.put(counter++);
            System.out.println(Thread.currentThread().getName()+"P:"+counter);
            Thread.sleep(20);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public void consume(){
        Integer take = null;
        try {
            take = queue.take();
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("C:"+take);
    }

    public static void main(String[] args) {
        PC_BlockingQueue pattern = new PC_BlockingQueue();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                while (true){
                    pattern.produce();
                }
            },i+"").start();
        }

        for (int i = 0; i < 1; i++) {
            new Thread(()->{
                while (true){
                    pattern.consume();
                }
            }).start();
        }
    }

}
